﻿

using IotContract.Interfaces.Process;
using IotContract.Models.Process;
using IotContract.Models.Process.Zd;
using Mapster;
using F29ServerProcess.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IotContract.Enums;
using IotContract.Models.Process.F29;
using IotContract.Models.Jobs.F29;
using System.Collections.Concurrent;
using IotContract.Models.Jobs;
using IotContract.Dtos.Remotes.Request;
using System.Data.Common;

namespace F29ServerProcess;
public class ProcessOpenProvider : IProcessOpenProvider
{
    private readonly IProcessDataService _processDataService;
    private readonly IProcessJobService _processJobService;
    public ProcessOpenProvider(IProcessDataService processDataService, IProcessJobService processJobService)
    {
        _processDataService = processDataService;
        _processJobService = processJobService;
    }

    #region 任务
    /// <summary>
    /// 获取任务数据
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public List<ProcessBaseData> GetJobs(ProcessProviderType type)
    {
        List<ProcessBaseData> datas = new List<ProcessBaseData>();
        switch (type)
        {
            case ProcessProviderType.None:
                break;
            case ProcessProviderType.EnterJob:
               datas=  _processJobService.GetJobs(ProcessProviderType.EnterJob);
                break;
            case ProcessProviderType.OutJob:
               datas= _processJobService.GetJobs(ProcessProviderType.OutJob);
                break;
            case ProcessProviderType.MoveJob:
               datas= _processJobService.GetJobs(ProcessProviderType.MoveJob);
                break;
            case ProcessProviderType.FireJob:
               datas= _processJobService.GetJobs(ProcessProviderType.FireJob);
                break;
            case ProcessProviderType.FmtJob:
               datas= _processJobService.GetJobs(ProcessProviderType.FmtJob);
                break;
            default:
                break;
        }
        return datas;
    }

    /// <summary>
    /// 完成任务
    /// </summary>
    /// <param name="fmsJobCompleteRequest"></param>
    /// <returns></returns>
   public async Task<string> CompleteJob(FmsJobCompleteRequest fmsJobCompleteRequest)
    {
      var result= await _processDataService.CompleteJob(fmsJobCompleteRequest.TaskNumberId, fmsJobCompleteRequest.TaskNumberId2);

        if (result.Success)
        {
            _processJobService.RemoveJob(fmsJobCompleteRequest.GetKey());
            // 移除
            return string.Empty;
        }

        return result.Message;


    }
    /// <summary>
    /// 添加任务
    /// </summary>
    /// <param name="job"></param>
    /// <param name="type">任务类型 2:入库 3:出库 4:移库 5:火警任务 6:充放电</param>
    /// <returns>错误消息，为空成功</returns>
    public string AddJob(ProcessBaseData job,ProcessProviderType type)
    {
        return _processJobService.AddJob(job, type);
    }
    /// <summary>
    /// 删除任务
    /// </summary>
    /// <param name="job"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public string RemoveJob(ProcessBaseData job, ProcessProviderType type)
    {
        return _processJobService.RemoveJob(job, type);
    }
    #endregion





    #region 数据
    /// <summary>
    /// 获取堆垛机数据
    /// </summary>
    /// <returns></returns>
    public List<ProcessBaseData> GetStackers()
    {
        var datas = _processDataService.ScUnits.Values.ToList();
        List<ProcessBaseData> scUnits = new List<ProcessBaseData>();

        foreach (var data in datas)
        {

            F29ProcessScData cv = data.Adapt<F29ProcessScData>();
            cv.Num = data.EquipNum;

            scUnits.Add(cv);
        }

        return scUnits;
    }
    /// <summary>
    /// 获取物流线数据
    /// </summary>
    /// <returns></returns>
    public List<ProcessBaseData> GetUnitCVs()
    {
        var datas = _processDataService.CvUnitTags.Values.ToList();
        List<ProcessBaseData> cvUnits = new List<ProcessBaseData>();

        foreach (var data in datas)
        {

            F29ProcessCvData cv = data.Adapt<F29ProcessCvData>();
            cv.Num = data.TagNum.ToString();

            cvUnits.Add(cv);
        }


      

        return cvUnits;
    }

    /// <summary>
    /// 根据类型获取其它设备数据
    /// </summary>
    /// <param name="iotEquipType"></param>
    /// <returns></returns>
    public List<ProcessBaseData> GetOthers(IotEquipType iotEquipType)
    {
        List<ProcessBaseData> datas = new List<ProcessBaseData>();
        var equips = _processDataService.GetAllEquips();

        equips.ForEach(x =>
        {
            datas.Add(x);
        });
        return datas;
    }
    /// <summary>
    /// 获取拘束机数据
    /// </summary>
    /// <returns></returns>
    private List<ProcessBaseData> GetRestraints()
    {
        var datas = _processDataService.RestraintUnits.Values.ToList();
        List<ProcessBaseData> restraintUnits = new List<ProcessBaseData>();

        foreach (var data in datas)
        {

            F29ProcessRestraintData cv = data.Adapt<F29ProcessRestraintData>();
            cv.Num = data.EquipNum.ToString();

            restraintUnits.Add(cv);
        }

        return restraintUnits;
    }
    #endregion

    /// <summary>
    /// 执行相关数据
    /// </summary>
    /// <param name="job"></param>
    /// <param name="type"></param>
    public async Task ExecuteData(ProcessBaseData job,ProcessProviderType type)
    {
        await _processDataService.ExecuteData(job, type);
    }

}
